﻿function Get-NetConnectionId {
<#
.SYNOPSIS
Get NetConnectionId using Windows Management Instrumentation (WMI)
.DESCRIPTION
Get NetConnectionId using Windows Management Instrumentation (WMI) from all Network Interface Cards
.PARAMETER ComputerName
The computer to perform action against. Defaults to the local computer if not specified. Accepts ValueFromPipeline and ValueFromPipelineByPropertyName.
.PARAMETER NetConnectionId
Only NICs matching the specified NetConnectionId will be modified
.PARAMETER DefaultGatewayPresentOnly
Switch-parameter to get NIC with a default gateway present only
.PARAMETER Logfile
Path to log-file (only errors is logged)
.EXAMPLE
Get-NetConnectionId -Computername SRV01
.EXAMPLE
Get-NetConnectionId -ComputerName (Get-Content c:\computernames.txt)
.EXAMPLE
Import-Module ActiveDirectory;Get-ADComputer -filter * | Select-Object @{l='computername';e={$_.Name}} | Get-NetConnectionId
.EXAMPLE
Get-NetConnectionId -DefaultGatewayPresentOnly
.NOTES
AUTHOR:    Jan Egil Ring
BLOG:      http://blog.powershell.no
LASTEDIT:  15.10.2011
#Requires -Version 2.0
#>

    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
        [string[]]$ComputerName = "localhost",
        [switch]$DefaultGatewayPresentOnly,
        [string]$Logfile
    )
    BEGIN {
    
       if ($logfile) {
        New-Item -Path $Logfile -ItemType file -Force | Out-Null
        }                   

    }
    PROCESS {
        if ($PSBoundParameters.ContainsKey('computername')) {
            foreach ($computer in $computername) {
                Write-Verbose 'Input from parameter'
                Get-NetConnectionIdWorker -computername $computername
            }
        } else {
            Write-Verbose 'Input from pipeline'
            Get-NetConnectionIdWorker -computername $computername
        }
    }
}

#Helper function
function Get-NetConnectionIdWorker {
    param($computername)
    $continue = $True
    try {
        if ((Test-Connection -ComputerName $computername -Count 1 -Quiet) -and ($computersystem = Get-WmiObject -Class win32_computersystem -Computername $computername -ErrorAction stop)) {
        Write-Verbose "Connected to computer $computername"
        $NICs = Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE" -ComputerName $computername -ErrorAction Stop
        
        if ($DefaultGatewayPresentOnly) {
        Write-Verbose 'Filtering NICs with default gateway present'
        $NICs = $NICs | Where-Object {$_.DefaultIPGateway}
        }
                
        if ($NICs) {
        
        $nicinfo = @{}
        $IPAdress = @()
        $NetConnectionId = @()
        $Description = @()
        
        foreach ($NIC in $NICs) {
        
        Write-Verbose "Processing NIC $($NIC.Description)"
        $IPAddress += $NIC.IPAddress
        $NetConnectionId += (Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapter WHERE Index = $($NIC.Index)" -ComputerName $computername -ErrorAction Stop).NetConnectionId
        $Description += $NIC.Description
        
        }
        
        $nicinfo.IPAddress = $IPAddress
        $nicinfo.NetConnectionId = $NetConnectionId
        $nicinfo.Description = $Description
        }
      }
    } 
    
    catch {
        Write-Verbose "An error occured connecting to computer $computername"
        Write-Verbose $error[0].exception

        if ($logfile) {
        $computername | Out-File -FilePath $Logfile -Append
        $error[0].exception | Out-File -FilePath $Logfile -Append
        }
    }
        
        Write-Verbose "Writing output object"
                
        $computerinfo = @{}
        $computerinfo.ComputerName = $computersystem.name
        $computerinfo.NICCount = ($NICs | Measure-Object).count
        $computerinfo.NetConnectionID = ($nicinfo | Select-Object @{Name='NetConnectionId';Expression={[string]::join(";", ($_.NetConnectionId))}}).NetConnectionId
        $computerinfo.NICDescription = ($nicinfo | Select-Object @{Name='Description';Expression={[string]::join(";", ($_.Description))}}).Description
        $computerinfo.IPAddress = ($nicinfo | Select-Object @{Name='IPAddress';Expression={[string]::join(";", ($_.IPAddress))}}).IPAddress
                                
        $obj = New-Object -TypeName PSObject -Property $computerinfo

        Write-Output $obj | Select-Object ComputerName,NICCount,NetConnectionID,NICDescription,IPAddress
}